package org.cfx.gift.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.ApiOperation;
import org.cfx.gift.domain.Employee;
import org.cfx.gift.dto.TenantDto;
import org.cfx.gift.dto.TenantEmailDto;
import org.cfx.gift.service.IEmployeeService;
import org.cfx.gift.service.ITenantService;
import org.cfx.gift.domain.Tenant;
import org.cfx.gift.query.TenantQuery;
import org.cfx.gift.util.AjaxResult;
import org.cfx.gift.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author RTX 9090
 */
@RestController
@RefreshScope  //刷新配置
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;

    @Autowired
    private IEmployeeService employeeService;

    @PutMapping("/notPass")
    public AjaxResult update(@RequestBody TenantEmailDto shopEmailDto) {

        try {
            tenantService.sendNotPassEmail(shopEmailDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("失败！管理员不存在或邮箱为空！").setSuccess(false);
        }

    }

    @PutMapping("/pass")
    public AjaxResult pass(@RequestBody TenantEmailDto shopEmailDto) {

        try {
            tenantService.sendPassEmail(shopEmailDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("失败！管理员不存在或邮箱为空！").setSuccess(false);
        }

    }

    @GetMapping("/updateByUUID/{uuid}")
    public AjaxResult updateByUUID(@PathVariable("uuid") String uuid) {
        System.out.println(uuid);
        TenantDto tenantDto = tenantService.queryByUUId(uuid);
        return new AjaxResult().setResultObj(tenantDto);
    }

    @GetMapping("/activateCheck/{number}")
    public AjaxResult activateCheck(@PathVariable("number") String number) {
        //redis查询key(激活码)是否存在
        tenantService.check(number);
        return new AjaxResult();
    }

    @GetMapping("/check/{username}")
    public AjaxResult check(@PathVariable("username") String username) {

        Employee one = employeeService.selectOne(new EntityWrapper<Employee>().eq("username", username));
        if (one != null) {
            throw new RuntimeException("管理员账号已存在！");
        }
        return new AjaxResult();
    }

    @GetMapping("/checkShop/{name}")
    public AjaxResult checkShop(@PathVariable("name") String name) {

        Tenant one = tenantService.selectOne(new EntityWrapper<Tenant>().eq("companyName", name));

        if (one != null) {

            throw new RuntimeException("店铺名称已存在！");
        }
        return new AjaxResult();

    }

    //店铺入驻
    @PostMapping("/settlement")
    public AjaxResult tenantRegister(@RequestBody @Valid TenantDto dto) {
        tenantService.tenantRegister(dto);
        return AjaxResult.me().setSuccess(true);
    }

    /**
     * 保存和修改公用的
     *
     * @param tenant 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant) {
        if (tenant.getId() != null) {
            tenantService.updateById(tenant);
        } else {
            tenantService.insert(tenant);
        }
        return AjaxResult.me();
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        tenantService.deleteById(id);
        return AjaxResult.me();
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        Tenant tenant = tenantService.selectById(id);
        return AjaxResult.me().setResultObj(tenant);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult list() {
        List<Tenant> list = tenantService.selectList(null);
        return AjaxResult.me().setResultObj(list);
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody TenantQuery query) {
        Page<Tenant> page = new Page<Tenant>(query.getPage(), query.getRows());
        page = tenantService.selectPage(page, new EntityWrapper<Tenant>().like("company_name", query.getKeyword()));
        PageList<Tenant> pageList = new PageList<Tenant>(page.getTotal(), page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    @PatchMapping("/deleteList")
    @ApiOperation(value = "批量删除用户")
    public AjaxResult deleteList(@RequestBody ArrayList<Long> ids) {
        tenantService.deleteBatchIds(ids);
        return new AjaxResult();
    }
}
